package com.lordjoe.collectiveintelligence;

/**
 * com.lordjoe.collectiveintelligence.PairId
 *  This is a class that uses a pair of object as
 * a single ID -
 * NOTE it may break if the items change their hash code
 * @author Steve Lewis
 * @date Feb 24, 2009
 */
public class CollectiveId<T>
{
    public static CollectiveId[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = CollectiveId.class;

    private final T[] m_Items;

    public CollectiveId(T... i1)
    {
        for (int i = 0; i < i1.length; i++) {
            T t = i1[i];
            if(t == null)
                throw new IllegalArgumentException("null not allowed"); 
        }
         m_Items = i1.clone();
    }

    public T[] getItems()
    {
        return m_Items;
    }



    @Override
    public boolean equals(Object test)
    {
        if(test == this)
            return true;
        if(test == null)
             return false;
        if(test.getClass() != getClass())
             return false;
          CollectiveId<T> realTest = (CollectiveId<T>)test;
        T[] ts = realTest.getItems();
        T[] items = getItems();
        if(ts.length != items.length)
            return false;
        for (int i = 0; i < items.length; i++) {
          if(ts[i].equals(items[i]))
              return false;
        }
        return true;
    }

    @Override
     public int hashCode()
    {
        T[] items = getItems();
        int ret = 0;
        for (int i = 0; i < items.length; i++) {
            ret ^= items[i].hashCode();
        }
        return ret;
    }

}